/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.runtime.impl.goal;

import prototype.persistence.pojo.Scenario;
import prototype.persistence.pojo.User;
import prototype.persistence.pojo.UserHasUnit;
import prototype.runtime.federate.RuntimeUser;
import prototype.runtime.interfaces.RuntimeGoal;

/**
 *
 * @author Server
 */
public class ProtectCiviliansGoal extends AbstractRuntimeGoal implements RuntimeGoal {

    private static String name = "Protect Civilians";
    private static String description = "Prevent death of civilians, only %i civilian deaths \nare allowed.";

    public boolean check(Scenario scenario, RuntimeUser runtimeUser) {
        if (status == AbstractRuntimeGoal.ACCOMPLISHED) {
            return true;
        } else if (status == AbstractRuntimeGoal.FAILED) {
            return false;
        }
        int deadCivs = 0;
        for (User u : scenario.getUsers()) {
            if (u.getTeam() == 2) {
                for (UserHasUnit uhu : u.getUserHasUnits()) {
                    if (uhu.getUnit().getHealth() < 0) {
                        deadCivs++;
                    }
                }
            }
        }
        if (deadCivs <= getGoal().getCivilLimit()) {
            AnnihilateEnemyGoal annenGoal = new AnnihilateEnemyGoal();
            if (annenGoal.check(scenario, runtimeUser)) {
                setStatus(AbstractRuntimeGoal.ACCOMPLISHED);
                return true;
            } else if (annenGoal.status != AbstractRuntimeGoal.ONGOING) {
                setStatus(AbstractRuntimeGoal.ACCOMPLISHED);
                return true;
            } else {
                setStatus(AbstractRuntimeGoal.ONGOING);
                return false;
            }
        } else {
            setStatus(AbstractRuntimeGoal.FAILED);
            return false;

        }
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description.replaceFirst("%i", String.valueOf(getGoal().getCivilLimit()));
    }
}
